package receiver.screens;

import java.net.URI;
import java.net.URISyntaxException;

import org.apache.http.impl.client.DefaultHttpClient;

import edu.washington.cs.sounddetector.server.api.SoundDetectorAPI;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

public class C2DMReceiver extends BroadcastReceiver {
	private static String lastID;

	@Override
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction();
    	if("com.google.android.c2dm.intent.REGISTRATION".equals(action)) {
    		//id = intent.getStringExtra("registration_id");
    		if((intent.getStringExtra("error")) != null)
    			Log.e("C2DM", "error registering with c2dm");
    		else {
    			Log.e("WOOOOOOOT", "IT WORKED!");
    			lastID = intent.getStringExtra("registration_id");
    			send(context);
    			//main.setupReceive(intent.getStringExtra("registration_id"));
    		}
    	} else if(intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE")) {
    		Log.e("SECRET MESSAGE", intent.getStringExtra("zzz"));
    		if(lastID != null) {
    			send(context);
    		}
    			
    		//main.getData();
    	}
	}
	
	private void send(Context context) {
			Intent i = new Intent(context, ReceiverPhoneActivity.class);
			i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			i.putExtra("cloudID", lastID);
		
			if(lastID != null)
				context.startActivity(i);		
	}
	
}
